home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / remote.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  5KB  |  144 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import os
  5. from quodlibet.util import fifo
  6. from quodlibet import const
  7.  
  8. try:
  9.     from quodlibet.util import winpipe
  10. except ImportError:
  11.     winpipe = None
  12.  
  13.  
  14. class RemoteError(Exception):
  15.     pass
  16.  
  17.  
  18. class RemoteBase(object):
  19.     '''A thing for communicating with existing instances of ourself.'''
  20.     
  21.     def __init__(self, app, cmd_registry):
  22.         '''Takes an Application and CommandRegistry'''
  23.         raise NotImplemented
  24.  
  25.     
  26.     def remote_exists(self):
  27.         '''See if another instance exists'''
  28.         raise NotImplemented
  29.  
  30.     remote_exists = classmethod(remote_exists)
  31.     
  32.     def send_message(cls, message):
  33.         """Send data to the existing instance if possible and returns
  34.         a response.
  35.  
  36.         Raises RemoteError in case the message couldn't be send or
  37.         there was no response.
  38.         """
  39.         raise NotImplemented
  40.  
  41.     send_message = classmethod(send_message)
  42.     
  43.     def start(self):
  44.         '''Start the listener for other instances'''
  45.         raise NotImplemented
  46.  
  47.     
  48.     def stop(self):
  49.         '''Stop the listener for other instances'''
  50.         raise NotImplemented
  51.  
  52.  
  53.  
  54. class QuodLibetWinRemote(RemoteBase):
  55.     _NAME = 'quodlibet'
  56.     
  57.     def __init__(self, app, cmd_registry):
  58.         self._app = app
  59.         self._cmd_registry = cmd_registry
  60.         self._server = winpipe.NamedPipeServer(self._NAME, self._callback)
  61.  
  62.     
  63.     def remote_exists(cls):
  64.         return winpipe.pipe_exists(cls._NAME)
  65.  
  66.     remote_exists = classmethod(remote_exists)
  67.     
  68.     def send_message(cls, message):
  69.         
  70.         try:
  71.             winpipe.write_pipe(cls._NAME, message)
  72.         except EnvironmentError:
  73.             e = None
  74.             raise RemoteError(e)
  75.  
  76.  
  77.     send_message = classmethod(send_message)
  78.     
  79.     def start(self):
  80.         self._server.start()
  81.  
  82.     
  83.     def stop(self):
  84.         self._server.stop()
  85.  
  86.     
  87.     def _callback(self, data):
  88.         self._cmd_registry.handle_line(self._app, data)
  89.  
  90.  
  91.  
  92. class QuodLibetUnixRemote(RemoteBase):
  93.     _PATH = const.CONTROL
  94.     
  95.     def __init__(self, app, cmd_registry):
  96.         self._app = app
  97.         self._cmd_registry = cmd_registry
  98.         self._fifo = fifo.FIFO(self._PATH, self._callback)
  99.  
  100.     
  101.     def remote_exists(cls):
  102.         return fifo.fifo_exists(cls._PATH)
  103.  
  104.     remote_exists = classmethod(remote_exists)
  105.     
  106.     def send_message(cls, message):
  107.         
  108.         try:
  109.             return fifo.write_fifo(cls._PATH, message)
  110.         except EnvironmentError:
  111.             e = None
  112.             raise RemoteError(e)
  113.  
  114.  
  115.     send_message = classmethod(send_message)
  116.     
  117.     def start(self):
  118.         self._fifo.open()
  119.  
  120.     
  121.     def stop(self):
  122.         self._fifo.destroy()
  123.  
  124.     
  125.     def _callback(self, data):
  126.         
  127.         try:
  128.             (data, path) = fifo.split_message(data)
  129.         except ValueError:
  130.             for line in data.splitlines():
  131.                 self._cmd_registry.handle_line(self._app, line)
  132.             
  133.  
  134.         with open(path, 'wb') as h:
  135.             response = self._cmd_registry.handle_line(self._app, data)
  136.             if response is not None:
  137.                 h.write(response)
  138.  
  139.  
  140. if os.name == 'nt':
  141.     Remote = QuodLibetWinRemote
  142. else:
  143.     Remote = QuodLibetUnixRemote
  144.